package cn.kgc.zuxin.service.impl;

import cn.kgc.zuxin.dto.DishDto;
import cn.kgc.zuxin.entity.Dish;
import cn.kgc.zuxin.entity.DishFlavor;
import cn.kgc.zuxin.exception.CustomException;
import cn.kgc.zuxin.mapper.DishMapper;
import cn.kgc.zuxin.service.DishFlavorService;
import cn.kgc.zuxin.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品同时保存口味数据
     *
     * @param dishDto
     */

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {

        //保存菜品基本信息
        this.save(dishDto);

        //菜品id
        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().peek((item) -> item.setDishId(dishId)).collect(Collectors.toList());

        //保存菜品口味数据
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    /**
     * 根据id查询菜品信息及对应口味
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //条件构造器
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //更新菜品信息
        this.updateById(dishDto);

        //清理当前菜品口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors.stream().peek((item) -> item.setDishId(dishDto.getId())).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

    }

    @Override
    @Transactional
    public void deleteById(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(ids != null, Dish::getId, ids);

        List<Dish> list = this.list(queryWrapper);

        for (Dish dishItem : list) {
            Integer status = dishItem.getStatus();
            if (status == 0) {
                this.removeById(dishItem.getId());
            } else {
                throw new CustomException("删除菜品有正在售卖的菜品,无法全部删除");
            }
        }
    }
}
